package qianfeng.com.meijiadaka;


import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import qianfeng.com.meijiadaka.bean.Find_list;
import qianfeng.com.meijiadaka.fragment.NewFragment;
import qianfeng.com.meijiadaka.fragment.OtherFragment;
import qianfeng.com.meijiadaka.interfacebean.FindListData;
import qianfeng.com.meijiadaka.interfacebean.HomeInfo;
import retrofit.GsonConverterFactory;
import retrofit.Retrofit;
import retrofit.RxJavaCallAdapterFactory;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

public class FindImageActivity extends AppCompatActivity {

    @BindView(R.id.find_ib)
    ImageButton mFindIb;
    @BindView(R.id.find_tl)
    TabLayout mFindTl;
    @BindView(R.id.find_vp)
    ViewPager mFindVp;
    @BindView(R.id.find_tv)
    TextView mFindTv;
    private List<String>mTitle;
    private List<Fragment>mFragment;
    private String mBaseUrl="http://home.naildaka.com/";
    private String mUrl="http://home.naildaka.com/vapi/v4/home/picture/list";
    private MyPagerAdapter mMyPagerAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_find_image);
        ButterKnife.bind(this);
        mFragment=new ArrayList<>();
        mTitle=new ArrayList<>();
        mMyPagerAdapter = new MyPagerAdapter(getSupportFragmentManager());
        initData();
    }
    private void initData()
      {
          mTitle.clear();
          mFragment.clear();
          new Retrofit.Builder().baseUrl(mBaseUrl).
            addCallAdapterFactory(RxJavaCallAdapterFactory.create())
            .addConverterFactory(GsonConverterFactory.create()).build().create(FindListData.class)
            .getData(mUrl).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).
                subscribe(new Action1<Find_list>() {
                    @Override
                    public void call(Find_list find_list) {
                        List<Find_list.ResultBean> result = find_list.getResult();
                        for (int i = 0; i < result.size(); i++) {
                            mTitle.add(result.get(i).getStyleName());
                            mFragment.add(OtherFragment.getFragment(result.get(i).getStyleId()));
                        }
                        mTitle.add(0,"最新");
                        mFragment.add(0,new NewFragment());
                        mFindTl.setTabTextColors(Color.GRAY,Color.RED);
                        mFindTl.setSelectedTabIndicatorHeight(2);
                        mFindTl.setBackgroundColor(Color.WHITE);
                        mFindVp.setAdapter(mMyPagerAdapter);
                        mFindTl.setupWithViewPager(mFindVp);
                        mFindVp.setOffscreenPageLimit(5);
                        mFindTl.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
                            @Override
                            public void onTabSelected(TabLayout.Tab tab) {
                                mFindVp.setCurrentItem(tab.getPosition());
                            }
                            @Override
                            public void onTabUnselected(TabLayout.Tab tab) {

                            }
                            @Override
                            public void onTabReselected(TabLayout.Tab tab) {

                            }
                        });
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {

                    }
                });
    }


    @OnClick({R.id.find_tv, R.id.find_ib})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.find_tv:
                //跳转所有分类中
                startActivity(new Intent(this,AllActivity.class));
                break;
            case R.id.find_ib:
                finish();
                break;
        }
    }
    class MyPagerAdapter extends FragmentStatePagerAdapter {
        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public int getCount() {
            return mTitle.size();//页卡数
        }

        @Override
        public Fragment getItem(int position) {
            return mFragment.get(position);
        }
        @Override
        public CharSequence getPageTitle(int position) {
            return mTitle.get(position);//页卡标题
        }
    }
}


